package daily.oct;

import java.util.List;

public class lc2273 {
    public List<String> removeAnagrams(String[] words) {
        List<String> res = new java.util.ArrayList<>();
        res.add(words[0]);
        for (int i = 1; i < words.length; i++) {
            if (compare(words[i],words[i-1])==false) {
                res.add(words[i]);
            }
        }
        return res;
    }
    


    boolean compare(String a, String b) {
        /*
         * 通过做减法来记录每个字母在对应index上出现的次数
         * 如果最后每个index上都是0，说明两个字符串是字母异位词
         * 不用26个if判断，超级聪明
         */
        int[] cnt = new int[26];
        for (char c : a.toCharArray()) {
            cnt[c - 'a']++;
        }
        for (char c : b.toCharArray()) {
            cnt[c - 'a']--;
        }
        for (int i : cnt) {
            if (i != 0)
                return false;
        }
        return true;//true,两个字符串是字母异位词
    }
    
    public static void main(String[] args) {
        lc2273 lc = new lc2273();
        String[] words1 = {"abba","baba","bbaa","cd","cd"};
        System.out.println(lc.removeAnagrams(words1));
        String[] words2 = { "a", "b", "c", "d", "e" };
        System.out.println(lc.removeAnagrams(words2));
    }
    
}
